ABCDEFGHIJKLMNOPQRSTVWXYZ abcdefghijklmnopqrstuvwxyz

now I know my ABC’s, what’s next?

library(statexpress)
library(tidyverse)

update_geom_defaults(GeomPoint, aes(size = from_theme(pointsize * 3)))

encode <- function(color, ...){
  aes(color = {{color}}, fill = {{color}}, ...) 
}

use <- encode


use_x <- function(x){list(aes(x = {{x}}))}
use_y <- function(y){list(aes(y = {{y}}))}




plot_data <- ggplot
use_weight <- function(weight){aes(weight = {{weight}})}
use_area <- function(area){aes(weight = {{area}})}
use_rows <- function(rows, cols, ...){facet_grid(rows = vars({{rows}}), cols = vars({{cols}}), ...)}

use_columns <- function(cols, rows, ...){facet_grid(rows = vars({{rows}}), cols = vars({{cols}}), ...)}
use_rows_columns <- function(rows, cols, ...){facet_grid(rows = vars({{rows}}), cols = vars({{cols}}), ...)}
use_wrap <- function(wrap, ...){facet_wrap(facets = vars({{wrap}}), ...)}
use_size <- function(size){aes(size = {{size}})}
use_shape <- function(shape){aes(shape = {{shape}})}
use_color <- function(color){aes(fill = {{color}})}
set_color <- function(color){aes(fill = I(color))}
use_color_line <- function(color){aes(color = {{color}})}
use_chart_point <- function(...){qlayer(geom = qproto_update(GeomPoint, aes(shape = 21), 
                                                         required_aes = c()),
                                    stat = qstat(function(data, scales){data$x <- data$x %||% 0 ; data$y <- data$y %||% 0; data}), ...)}


data <- function(data){ggplot(data |> remove_missing()) + theme_classic(ink = "darkgrey", paper = "whitesmoke", base_size = 18)}
chart_jitter <- geom_jitter

chart_heat <- function(...){list(
  qlayer(geom = GeomTile, 
         stat = qproto_update(StatSum, aes(fill = after_stat(n), size = NULL)), ...),
  scale_fill_gradientn(colors = c("blue", "white", "yellow", "orange", "red")),
  theme(panel.grid.minor = element_line(color = "darkgrey")))
}



title <- function(title){labs(title = title)}
subtitle <- function(subtitle){labs(subtitle = subtitle)}
caption <- function(caption){labs(caption = caption)}
tag <- function(tag){labs(tag = tag)}

pie of pets

chart_pie <- function(...){
  
  list(
  
  geom_bar(position = "fill", ...),
  aes(y = "all", color = from_theme(paper)),
  coord_polar(),
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.line = element_blank(),
        axis.title = element_blank()),
  labs(fill = NULL)
  
  )
  
}

theme_kids <- theme_classic(paper = "whitesmoke", 
              ink = "darkgrey", 
              base_size = 30) 


theme_set(theme_kids)


pets <- data.frame(pets = c("🐱", "🐶", "🦚", "🐠", "🐰"), 
                   number_of_pets = c(30, 25, 10, 15, 5)) |> 
  mutate(pets = fct_infreq(pets, number_of_pets) |> fct_rev())

head(pets)
#>   pets number_of_pets
#> 1   🐱             30
#> 2   🐶             25
#> 3   🦚             10
#> 4   🐠             15
#> 5   🐰              5


plot_data(pets) +
  use_color(pets) +
  use_area(number_of_pets) +
  chart_pie()



shuttles <- data.frame(shuttle = paste("🚀#", 1:6), gas = c(.3,.5,.3, .8,.7, .4))

plot_data(shuttles) +
  use_x(shuttle) +
  use_y(1) +
  geom_col(color = "black", fill = "transparent") + 
  geom_col(aes(y = gas), fill = "transparent", color = "black") + 
  geom_hline(yintercept = .75, linetype = "dashed")

A scatter of crustaceans

library(tidyverse)
types <- c("🦐", "🦀")
crustaceans <- cars |> 
  rename(size = dist) |>
  mutate(type = c(
    rep("🦐", 20),
    sample(types, 10, replace = T),
    rep("🦀", 20)))

head(crustaceans)
#>   speed size type
#> 1     4    2   🦐
#> 2     4   10   🦐
#> 3     7    4   🦐
#> 4     7   22   🦐
#> 5     8   16   🦐
#> 6     9   10   🦐

GeomPointFill <-qproto_update(GeomPoint, aes(shape = 21),
                              required_aes = c())


library(statexpress)
chart_point <- function(...){
  qlayer(geom = GeomPointFill,
         stat = qstat(function(data, scales){
           data$shape <- data$shape %||% data$picture
           data$x <- data$x %||% 0 ; 
           data$y <- data$y %||% 0; 
           data}), 
         ...)
  }

use_picture <- function(picture){aes(shape = I({{picture}}))}

plot_data(crustaceans) + 
  use(y = speed, x = size,
      picture = type, size = size) +
  chart_point() +
  labs(x = "little                        big") +
  labs(y = "slow           fast") + 
  use_picture(type)

A jungle bar chart… ?

theme_chart_bar <- function(){
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.ticks.x = element_blank())
  }

chart_bar <- function(...){
  list(geom_col(...), 
       theme_chart_bar(),
       scale_y_continuous(expand = expansion(c(0, .3))),
       labs(x = NULL))
}

compute_item_stack <- function(data, scales, width = 0.2){
               
    data$shape <- data$shape %||% data$picture

    data |> 
      uncount(y) |>
      dplyr::mutate(row = row_number()) |> 
      dplyr::mutate(y = row - 
        0.5) |>
      dplyr::mutate(width = width)
    
  }

chart_item_stack <- function(...){
  
  list(
  qlayer(
    geom = GeomPointFill, 
    stat = qstat(compute_item_stack)
  ),
  qlayer(
    geom = GeomTile, 
    stat = qstat(compute_item_stack), 
    alpha = 0
  ),
  scale_y_continuous(expand = expansion(c(0, .3))),
  labs(x = NULL)
  )

  
}

ggprop.test:::compute_group_bricks
#> function (data, scales, width = 0.2) 
#> {
#>     data %>% dplyr::mutate(row = row_number()) %>% dplyr::mutate(y = row - 
#>         0.5) %>% dplyr::mutate(width = width)
#> }
#> <bytecode: 0x1183bcbf8>
#> <environment: namespace:ggprop.test>

jungle <- data.frame(tree = paste0("🌴#", 1:5), 
                     num_bunches = c(2, 5, 1, 2, 1), 
                     banana = "🍌")

jungle |> 
  select(x = tree, y = num_bunches, picture = banana) |>
  compute_item_stack()
#>       x picture shape row    y width
#> 1  🌴#1      🍌    🍌   1  0.5   0.2
#> 2  🌴#1      🍌    🍌   2  1.5   0.2
#> 3  🌴#2      🍌    🍌   3  2.5   0.2
#> 4  🌴#2      🍌    🍌   4  3.5   0.2
#> 5  🌴#2      🍌    🍌   5  4.5   0.2
#> 6  🌴#2      🍌    🍌   6  5.5   0.2
#> 7  🌴#2      🍌    🍌   7  6.5   0.2
#> 8  🌴#3      🍌    🍌   8  7.5   0.2
#> 9  🌴#4      🍌    🍌   9  8.5   0.2
#> 10 🌴#4      🍌    🍌  10  9.5   0.2
#> 11 🌴#5      🍌    🍌  11 10.5   0.2


jungle |> 
  head()
#>   tree num_bunches banana
#> 1 🌴#1           2     🍌
#> 2 🌴#2           5     🍌
#> 3 🌴#3           1     🍌
#> 4 🌴#4           2     🍌
#> 5 🌴#5           1     🍌

plot_data(jungle) + 
  use(x = tree,
      y = num_bunches, 
      picture = banana) +
  chart_item_stack() + 
  annotate(geom = GeomText,
           x = I(.75), y = I(.72),
           label = "🎈🎀🙏",
           angle = -10,
           size = 22,
            )  



head(jungle)
#>   tree num_bunches banana
#> 1 🌴#1           2     🍌
#> 2 🌴#2           5     🍌
#> 3 🌴#3           1     🍌
#> 4 🌴#4           2     🍌
#> 5 🌴#5           1     🍌

plot_data(jungle) + 
  encode(x = tree,
         y = num_bunches) + 
  chart_bar()




knitr::knit_exit()